package com.chezai.client.activitys;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.chezai.client.global.DeviceFactory;
import com.chezai.client.inter.IMvpView;
import com.chezai.client.presenter.IPresenter;
import com.chezai.client.utils.AppManager;
import com.chezai.client.utils.TUtil;
import com.chezai.client.widget.ProgressBarDialog;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Activity基类
 * Created by likunyang on 2017/8/24.
 */

public abstract class BaseActivity<P extends IPresenter<V>, V extends IMvpView> extends AppCompatActivity implements IMvpView, BaseView {
    public String TAG = this.getClass().getSimpleName();
    protected P presenter;
    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        DeviceFactory.hardware.setScreenOrientation(this);
        unbinder = ButterKnife.bind(this);
        getPresenter();
        initView(savedInstanceState);
        initData();
        setListener();
    }

    private void getPresenter() {
        presenter = TUtil.getType(this, 0);
        if (presenter != null) {
            presenter.attachView((V) this);
            presenter.setContext(this);
        }
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setListener() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (presenter != null)
            presenter.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        if (presenter != null) {
            presenter.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        if (presenter != null) {
            presenter.onPause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (presenter != null)
            presenter.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.onDestroy();
            presenter.detachView();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }
        AppManager.getAppManager().finishActivity(this);
    }
}
